//
//  HDIdentitySelectVC.swift
//  HuaDaoPeifu
//
//  Created by ZERO on 2018/6/14.
//  Copyright © 2018年 paiyipai. All rights reserved.
// FIXME: - coding

import UIKit

class HDIdentitySelectVC: UIViewController {

    @IBOutlet weak var groupBtn: UIButton!
    
    @IBOutlet weak var personBtn: UIButton!
    
    @IBOutlet weak var exclusiveBtn: UIButton!
    
    var userType:HDUserType?
    // 跳转页面  0登录 1注册 2身份认证
    var ToWhere: Int?
    
    var isHaveBackBtn: Int?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        title = "速赔宝（角色选择）"

        initButton(btn: groupBtn)
        initButton(btn: personBtn)
        initButton(btn: exclusiveBtn)
 
        
        if (userType == .personal) {// 个人
            selectAction(personBtn)
        }else  if (userType == .exclusive) {// 专属
            selectAction(exclusiveBtn)
        }else {
            userType = .personal// 团体
            selectAction(groupBtn)
        }
        
        if isHaveBackBtn == 1{
        setBarButton()
        }

    }
    func setBarButton() {
       
            navigationItem.rightBarButtonItem = nil
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(backToGuide))
    }
    func backToGuide() {
        
        let backGuide = GuideViewController()
        UIApplication.shared.keyWindow?.rootViewController = backGuide;
        backGuide.scrollView.contentOffset.x = AppConstant.screenWidth*4
        backGuide.scrollView.isScrollEnabled = false
        UserDefaults.standard.set(false, forKey: "Guide")
        self.dismiss(animated: true, completion: nil)
        
    }
    
    
    
    func initButton(btn:UIButton) {
        // 图文居中
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment(rawValue: 0)!
        btn.titleEdgeInsets = UIEdgeInsetsMake((btn.imageView?.frame.size.height)! ,-(btn.imageView?.frame.size.width)!, -6.0,0.0)
        btn.imageEdgeInsets = UIEdgeInsetsMake(-18.0, 0.0,0.0, -(btn.titleLabel?.bounds.size.width)!)
        
        btn.layer.borderWidth = 1
        btn.layer.cornerRadius = 3
        btn.layer.borderColor = UIColor(hex: 0xdbdbdb).cgColor
    }
    
    @IBAction func selectAction(_ sender: UIButton) {
        if sender == groupBtn {
            groupBtn.setImage(UIImage(named: "tuantiyonghu"), for: UIControlState.normal)
            groupBtn.setTitleColor( UIColor(red:(255/255.0), green:(138/255.0), blue:(0/255.0), alpha:1.0),for:
                UIControlState())
            personBtn.setImage(UIImage(named: "gerenyonghu_un"), for: UIControlState.normal)
            personBtn.setTitleColor( UIColor.darkGray,for:
                UIControlState())
            exclusiveBtn.setImage(UIImage(named: "zhuanshuyonghu_un"), for: UIControlState.normal)
            exclusiveBtn.setTitleColor( UIColor.darkGray,for:
                UIControlState())
            userType = .group

        }else if sender == personBtn{
            personBtn.setImage(UIImage(named: "gerenyonghu"), for: UIControlState.normal)
            personBtn.setTitleColor( UIColor(red:(255/255.0), green:(138/255.0), blue:(0/255.0), alpha:1.0),for:
                UIControlState())
            exclusiveBtn.setImage(UIImage(named: "zhuanshuyonghu_un"), for: UIControlState.normal)
            exclusiveBtn.setTitleColor( UIColor.darkGray,for:
                UIControlState())
            groupBtn.setImage(UIImage(named: "tuantiyonghu-un"), for: UIControlState.normal)
            groupBtn.setTitleColor( UIColor.darkGray,for:
                UIControlState())
            
            userType = .personal

        }else if sender == exclusiveBtn{
            exclusiveBtn.setImage(UIImage(named: "zhuanshuyonghu"), for: UIControlState.normal)
            exclusiveBtn.setTitleColor( UIColor(red:(255/255.0), green:(138/255.0), blue:(0/255.0), alpha:1.0),for:
                UIControlState())
            personBtn.setImage(UIImage(named: "gerenyonghu_un"), for: UIControlState.normal)
            personBtn.setTitleColor( UIColor.darkGray,for:
                UIControlState())
            groupBtn.setImage(UIImage(named: "tuantiyonghu-un"), for: UIControlState.normal)
            groupBtn.setTitleColor( UIColor.darkGray,for:
                UIControlState())
            
            userType = .exclusive

        }
    }
    
    
    @IBAction func nextAction(_ sender: UIButton) {
        
        if ToWhere == 0{
            let loginVC = HDLoginNewVC()
            loginVC.userType = userType
            navigationController?.pushViewController(loginVC, animated: true)
        }else if ToWhere == 1{
            let registVC = HDRegistVC()
            registVC.accountType = userType
            
            navigationController?.pushViewController(registVC, animated: true)
        }else{
            let temArray = self.navigationController?.viewControllers
            var navi = HDidentityAuthenticationVC()
            
            
            let viewController = self.navigationController?.viewControllers[1] as! HDidentityAuthenticationVC
            viewController.accountType = userType

            self.navigationController?.popToViewController(viewController, animated: true)
        }
            

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
